<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mr_siswa extends CI_Controller {

	function __construct() {
    parent::__construct();
    $this->isLogin();	
		$this->load->model("master/master_siswa"); 		
		$this->load->model("master/master_saudara"); 		
		$this->load->model("master/master_ortuwali"); 		
		$this->load->model("master/master_pekerjaan"); 		
		$this->load->model("master/master_gaji"); 		
		$this->load->model("master/master_pendidikan"); 		
		$this->load->model("master/master_agama");
		$this->load->model("master/master_lembaga");  
		$this->load->model("master/master_tapel"); 
		$this->num_post=20;			
	}

	function isLogin(){
		$login=$this->session->userdata('isLogin');
		if(! isset($login)||$login != true){
			$this->session->set_flashdata('error_login', 'Untuk Mengakses Admin Anda Harus Login!');
			redirect('login');	
		}
	}
	
	function index($offset = 0)
	{
		$data['main_view']='master/mr_siswa/read';
		$data['aksi_cari']='mr_siswa/cari_siswa';
		$data['list_lembaga']=$this->master_lembaga->read();
		$uri_segment = 3;
		
		if($offset != 0){
			$offset = $this->uri->segment($uri_segment);
		}else{
			$offset=$offset;
		}
		$data['list'] = $this->master_siswa->limit($this->num_post, $offset);
		$data['offset']=$offset;
		$num_rows = $this->master_siswa->num_rows();
			
		if($num_rows>0){
			// Membuat pagination
			$config['base_url'] = base_url().'mr_siswa/index';
			$config['cur_tag_open'] = '<li class=active><a href="#">';
			$config['cur_tag_close'] = '<span class="sr-only">(current)</span></a></li>';
			$config['num_tag_open'] = '<li>';
			$config['num_tag_close'] = '</li>';
			$config['prev_link'] = '<';
			$config['prev_tag_open'] = '<li>';
			$config['prev_tag_close'] = '</li>';
			$config['next_link'] = '>';
			$config['next_tag_open'] = '<li>';
			$config['next_tag_close'] = '</li>';
			//buat last
			$config['last_tag_open'] = '<li>';
			$config['last_tag_close'] = '</li>';
			$config['last_link'] = '>>';
			//buat first
			$config['first_tag_open'] = '<li>';
			$config['first_tag_close'] = '</li>';
			$config['first_link'] = '<<';
			$config['total_rows'] = $num_rows;
			$config['per_page'] = $this->num_post;
			$config['uri_segment'] = $uri_segment;
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
		}
		$this->load->view('index',$data);
		
	}	

	function cari_siswa($offset = 0)
	{
		$data['main_view']='master/mr_siswa/read';
		$data['aksi_cari']='mr_siswa/cari_siswa';
		$data['lembaga']=$this->input->post('lembaga');
		$data['list_lembaga']=$this->master_lembaga->read();

		$id_lembaga = $this->input->post('lembaga');
		$keyword = $this->input->post('judul');

		$uri_segment = 3;
		
		if($offset != 0){
			$offset = $this->uri->segment($uri_segment);
		}else{
			$offset=$offset;
		}
		//buat cari
		if(isset($_POST['cari'])){
			$data['param_lembaga'] = $this->input->post('lembaga');
			$data['param_judul'] = $this->input->post('judul');
			$this->session->set_userdata('param_lembaga', $data['param_lembaga']);
			$this->session->set_userdata('param_judul', $data['param_judul']);
		} else {
			$data['param_lembaga'] = $this->session->userdata('param_lembaga');
			$data['param_judul'] = $this->session->userdata('param_judul');
		}
		
		$data['list'] = $this->master_siswa->limit_search($data['param_lembaga'],$data['param_judul'],$this->num_post, $offset);
		$data['offset']=$offset;
		$num_rows = $this->master_siswa->num_rows_search($data['param_lembaga'],$data['param_judul']);
			
		if($num_rows>0){
			// Membuat pagination
			$config['base_url'] = base_url().'mr_siswa/cari_siswa';
			$config['cur_tag_open'] = '<li class=active><a href="#">';
			$config['cur_tag_close'] = '<span class="sr-only">(current)</span></a></li>';
			$config['num_tag_open'] = '<li>';
			$config['num_tag_close'] = '</li>';
			$config['prev_link'] = '<';
			$config['prev_tag_open'] = '<li>';
			$config['prev_tag_close'] = '</li>';
			$config['next_link'] = '>';
			$config['next_tag_open'] = '<li>';
			$config['next_tag_close'] = '</li>';
			//buat last
			$config['last_tag_open'] = '<li>';
			$config['last_tag_close'] = '</li>';
			$config['last_link'] = '>>';
			//buat first
			$config['first_tag_open'] = '<li>';
			$config['first_tag_close'] = '</li>';
			$config['first_link'] = '<<';
			$config['total_rows'] = $num_rows;
			$config['per_page'] = $this->num_post;
			$config['uri_segment'] = $uri_segment;
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
		}
		$this->load->view('index',$data);
		
	}
	
	function add()
	{
		$id_lembaga = $this->session->userdata('id_lembaga');
		$lembaga = $this->master_lembaga->edit($id_lembaga);
		$kode = $lembaga[0]->kode_lembaga;
		$tingkat = substr($kode, 0,2);
		$data['tingkat'] = $tingkat;
		if($tingkat == 11){
			$thn = date('Y');
			$bulan = date('m');
			$tahun = date('y');

			$data['nva'] = "";
			$data['nis'] = "";
		}else if($tingkat == 22){
			$tahun = date('y');
			$bulan = date('m');

			$max = $this->master_siswa->nis($kode);
			$idMax = $max['nis'];
			$noUrut = (int) $idMax;
			$noUrut++;
			$nisMax = sprintf("%04s", $noUrut);
			$data['nva'] = "$tahun$bulan$nisMax";
			$data['nis'] = "$kode$tahun$nisMax";
		}

		$data['main_view']='master/mr_siswa/add';
		$this->load->view('index',$data);
	}
	
	function add_exe()
	{
		$tgl = $this->input->post('tgl_lahir');
		$tglMySQL = date("Y-m-d", strtotime ($tgl));
		$tapel = $this->master_tapel->get_tapel_aktif();
		$dataInsert = array(
			'nis' => $this->input->post('nis'),
			'nva' => $this->input->post('nva'),
			'nik' => $this->input->post('nik'),
			'nm_lengkap' => $this->input->post('nm_lengkap'),
			'jk' => $this->input->post('jk'),
			'nm_panggilan' => $this->input->post('nm_panggilan'),
			'tmp_lahir' => $this->input->post('tmp_lahir'),
			'tgl_lahir' => $tglMySQL,
			'alamat' => $this->input->post('alamat'),
			'tlp' => $this->input->post('tlp'),
			'email' => $this->input->post('email'),
			'jarak' => $this->input->post('jarak'),
			'trans' => $this->input->post('trans'),
			'foto' => $this->input->post('foto'),
			'asal_sekolah' => $this->input->post('asal_sekolah'),
			'almt_sekolah' => $this->input->post('almt_sekolah'),
			'id_tapel_masuk' => $tapel['id_mr_th_ajaran'],
			'id_lembaga' => $this->session->userdata('id_lembaga')
		);
		$this->master_siswa->add($dataInsert);		
		redirect ('mr_siswa');
	}	
	
	function edit()
	{
		$nis = $this->security->xss_clean($this->uri->segment(3));	
		$data['list']=$this->master_siswa->edit($nis);
		$data['lembaga']=$this->master_siswa->get_lembaga();		
		$data['main_view']='master/mr_siswa/edit';
		$this->load->view('index',$data);
	}
	
	function update()
	{
		$nis=$this->input->post('nva_lama');
		$tgl = $this->input->post('tgl_lahir');
		$tglMySQL = date("Y-m-d", strtotime ($tgl));
		
		$dataInsert=array(
			'nik' => $this->input->post('nik'),
			'nis' => $this->input->post('nis'),
			'nva' => $this->input->post('nva'),
			'nm_lengkap' => $this->input->post('nm_lengkap'),
			'jk' => $this->input->post('jk'),
			'nm_panggilan' => $this->input->post('nm_panggilan'),
			'tmp_lahir' => $this->input->post('tmp_lahir'),
			'tgl_lahir' => $tglMySQL,
			'alamat' => $this->input->post('alamat'),
			'tlp' => $this->input->post('tlp'),
			'email' => $this->input->post('email'),
			'jarak' => $this->input->post('jarak'),
			'trans' => $this->input->post('trans'),
			'foto' => $this->input->post('foto'),
			'asal_sekolah' => $this->input->post('asal_sekolah'),
			'almt_sekolah' => $this->input->post('almt_sekolah'),
			'id_lembaga' => $this->input->post('lembaga'),
		);
		$this->master_siswa->update($nis,$dataInsert);
		redirect('mr_siswa');
	}	
	
	function delete()
	{
		$nis = $this->security->xss_clean($this->uri->segment(3));
		$this->master_siswa->delete($nis);
		redirect('mr_siswa');
	}
	
	function detail()
	{
		$nis = $this->security->xss_clean($this->uri->segment(3));	
		$data['list']=$this->master_siswa->detail($nis);		
		$data['list_saudara']=$this->master_saudara->detail($nis);		
		$data['list_ortuwali']=$this->master_ortuwali->detail($nis);	
		//print_r($data['list_ortuwali']);

		
		$data['list_pekerjaan']=$this->master_pekerjaan->read();		
		$data['list_gaji']=$this->master_gaji->read();		
		$data['list_pendidikan']=$this->master_pendidikan->read();		
		$data['list_agama']=$this->master_agama->read();		
		$data['main_view']='master/mr_siswa/detail';
		$this->load->view('index',$data);
	}
	
}